//
//  StatusOriginaView.swift
//  Weibo
//
//  Created by gdarkness on 16/3/28.
//  Copyright © 2016年 gdarkness. All rights reserved.
//

import UIKit

import SDWebImage

class StatusOriginaView: UIView {
    
    //MARK: -属性
    var status: Status? {
        didSet{
            //当别人来设置微博模型的时候，我们根据微博模型来设置原创微博的相关内容
            //设置头像
            if let imageURL = status?.user?.profile_image_url{
                let url = NSURL(string: imageURL)
                iconView.sd_setImageWithURL(url,placeholderImage: UIImage(named: "avatar"))
            }
            //设置名称
            nameLabel.text = status?.user?.screen_name
            
            //设置时间
            timeLabel.text = status?.created_at
            
            //来源
            sourceLabel.text = status?.source
            
            //会员等级
            if let mbrank = status?.user?.mbrank{
                if mbrank > 0 && mbrank <= 6 {
                    memberView.image = UIImage(named: "common_icon_membership_level\(mbrank)")
                }else{
                    //防止cell服用，没有会员的就不要等级
                    memberView.image = nil
                }
            }
            //认证图标
            if let verified_type = status?.user?.verified_type{
                var image: UIImage?
                
                switch verified_type{
                case 0:
                    image = UIImage(named: "avatar_vip")
                case 2,3,5:
                    image = UIImage(named: "avatar_enterprise_vip")
                case 220:
                    image = UIImage(named: "avatar_grassroot")
                default:
                    image = nil
                }
                verifiedView.image = image
            }
            //设置微博内容
            contentLabel.text = status?.text
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor.random()
        prepareUI()
    }
    
    private func prepareUI(){
        //添加子控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        //添加约束
        
        //用户头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(StartusCellMargin)
            make.top.equalTo(self).offset(StartusCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        //用户名称
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(StartusCellMargin)
                make.top.equalTo(iconView)
        }
        
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(iconView.snp_trailing).offset(StartusCellMargin)
                make.bottom.equalTo(iconView)
        }
        
        //来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(StartusCellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        //会员等级
        memberView.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(nameLabel.snp_trailing).offset(StartusCellMargin)
            make.centerY.equalTo(nameLabel)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        //认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing)
            make.centerY.equalTo(iconView.snp_bottom)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        //微博文本内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_bottom).offset(StartusCellMargin
            )
            make.leading.equalTo(self).offset(StartusCellMargin)
            make.trailing.equalTo(self).offset(-StartusCellMargin)
        }
        
        //原创cell的底部参照contentLabel的底部
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(contentLabel).offset(StartusCellMargin)
        }
        
    }
    
    //MARK: - lazyload
    //用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    //用户名称
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        
        label.text = "超你妈"
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        
        return label
    }()
    
    //时间
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.text = "forever"
        label.textColor = UIColor.orangeColor()
        label.font = UIFont.systemFontOfSize(9)
        
        return label
    }()
    
    //来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel()
        label.text = "iPhone100"
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(9)
        
        return label
    }()
  
    //会员等级
    private lazy var memberView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_expired"))
    
    private lazy var verifiedView: UIImageView = UIImageView(image: UIImage(named: "avatar_grassroot"))

    
    
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(14)
        label.numberOfLines = 0
        
        return label
    }()

}
